#ifndef DATACENTER_H
#define DATACENTER_H

#include "src/scheduledatainfo.h"
#include <QPoint>

void notifyUser(QString content);

enum DisplayInfo {
    Passed = -1,  // 今天加上过去三天的所有任务
    Home = 0,   // 最近占用日的任务 + 过去三天未完成的任务 + 未来6天的提醒任务
    Day1, Day2, Day3, Day4, Day5, Day6, Day7    // 未来X天的任务
};

// 任务的唯一标识符
struct DataID
{
    int id;
    int recurId;
    QString title;
    DataID()
    {
        id = recurId = 0;
    }
    DataID(int a, int b, QString c)
    {
        id = a;
        recurId = b;
        title = c;
    }
};

inline bool operator==(const DataID &a, const DataID &b)
{
    return (a.id == b.id) && (a.recurId == b.recurId) && (a.title == b.title);
}

inline uint qHash(const DataID &key, uint seed)
{
    return qHash(key.title, seed) + uint(key.id * 100 + key.recurId);
}


// 任务的扩展信息：选中状态、置顶标志、倒计时标志
struct ExtendedData
{
    DataID id;

    bool isChecked = false;
    bool piningHint = false;
    bool countdownHint = false;

    ExtendedData() {}
    ExtendedData(bool a, bool b, bool c) {
        isChecked = a;
        piningHint = b;
        countdownHint = c;
    }
};

struct FloatedData
{
    QString title;
    QPoint location;
};

class QTimer;

class DataCenter : public QObject
{
    Q_OBJECT
public:
    static DataCenter& instance()
    {
        static DataCenter dc;
        return dc;
    }

    QPoint mainWindowPos;

    void loadData(QString filepath);
    void saveData();

    ExtendedData getExtendedData(const ScheduleDataInfo &schedule);
    void setExtendedData(const ScheduleDataInfo &schedule, const ExtendedData &extData);

    bool createFloatTask(QString title);
    bool removeFloatTask(QString title);
    void getFloatTask(QVector<FloatedData> &floatTasks);
    void setFloatTask(QString title, QPoint location);

    bool getJobs(const QDate &startDate, const QDate &endDate, QMap<QDate, QVector<ScheduleDataInfo> > &info);

private:
    DataCenter();
    DataCenter(const DataCenter&) = delete;
    DataCenter& operator=(const DataCenter&) = delete;

    QString m_filepath;
    QHash<DataID, ExtendedData> m_dataHash;
    QVector<FloatedData> m_floatTasks;

    QTimer *nextDayTimer;


public slots:
    void raiseCalendarWindow();
    void OpenSchedule(const QString &strjson);
    void getJobs(DisplayInfo displayInfo, QVector<ScheduleDataInfo> &schedules);

private slots:
    void onNextDay();

signals:
    void jobsUpdate();
    void timeUpdate();
    void extendedDataChanged();
};



#endif // DATACENTER_H
